package com.fit.dao.language;

import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.language.Language;
import com.fit.entities.user.User;

public class LanguageDAOImpl implements LanguageDAO {

	@Inject
	private Session session;

	@Log
	public boolean save(Language newLanguage) {
		if (newLanguage != null) {
			session.save(newLanguage);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(Language language) {
		if (language != null) {
			session.delete(language);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(Language language) {
		if (language != null) {
			session.update(language);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(Language.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		Language u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public Language findByID(Long id) {
		if (id != null) {
			return (Language) session.get(Language.class, id);
		}
		return null;
	}

	@Log
	public Language findByIdLanguageAndName(int languageId, String name) {
		if (languageId == 0 || name == null) {
			return null;
		}

		if (languageId==(0) || name.equals("")) {
			return null;
		}

		List l = session.createCriteria(Language.class)
				.add(Restrictions.eq("name", name))
				.add(Restrictions.eq("languageId", languageId)).list();
		Language u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (Language) l.get(0);
			}
		}
		return u;
	}

}
